import requests
import mysql.connector
import json

url = "http://api.xxmh365.com:8836/app/api/getComicInfo"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0",
    "Content-Type": "application/json;charset-UTF-8",
}
# # 继母
# bookid = 2 
# data = {
#     "comicId": "1513" 
# }

# # 女神
# bookid = 3 
# data = {
#     "comicId": "1687" 
# }

# 教授
bookid = 4 
data = {
    "comicId": "1631" 
}
conn = mysql.connector.connect(user='root', password='123456', database='cms')
cursor = conn.cursor()
resp = requests.post(url, headers=headers, data = json.dumps(data))
chapters = resp.json()['result']['chapterList']
for index,value in enumerate(chapters):
    cursor.execute('select * from cms_chapter where book_id = %s and chapter_id = %s', (bookid, index+1))
    count = len(cursor.fetchall())
    if count == 0:
        cursor.execute('insert into cms_chapter (book_id, chapter_id, chaptername) values (%s, %s, %s)', [bookid, index+1, value['chapterName']])
        print(str(index+1) + '-' + value['chapterName'])

conn.commit()
cursor.close()
conn.close()
